package code.easy.refresh.demo

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.requiredHeight
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import code.easy.refresh.BottomColumn
import code.easy.refresh.EasyRefresh
import code.easy.refresh.RefreshState

@Composable
fun MyRefresh(
    refreshState: RefreshState,
    onRefresh: () -> Unit,
    modifier: Modifier = Modifier,
    enabledRefresh: Boolean = true,
    content: @Composable () -> Unit,
) {
    EasyRefresh(
        refreshState = refreshState,
        onRefresh = onRefresh,
        modifier = modifier,
        enabledRefresh = enabledRefresh,
        enabledContentOnRefresh = false,
        refreshTriggerDistance = 60.dp,
        maxOffsetMultipleTrigger = 3.5f,
        refreshAnimDuration = 300,
        finishAnimDuration = 300,
        refreshIndicator = { _state, _offsetTop ->
            MyRefreshIndicator(_state, _offsetTop)
        },
        content = content
    )
}

@Composable
fun BoxScope.MyRefreshIndicator(state: RefreshState.State, offsetTop: Dp) {
    var text by remember { mutableStateOf("下拉刷新") }
    LaunchedEffect(key1 = state) {
        text = when (state) {
            RefreshState.State.Null -> "下拉刷新"
            RefreshState.State.Pull -> "下拉刷新"
            RefreshState.State.Refresh -> "刷新中。"
            RefreshState.State.Finish -> "刷新完毕"
            RefreshState.State.Close -> "下拉刷新"
        }
    }
    BottomColumn {
        Box(
            contentAlignment = Alignment.Center,
            modifier = Modifier
                .requiredHeight(60.dp)
                .fillMaxWidth()
                .background(Color.Cyan)
        ) {
            Text(text = text)
        }
    }
}